<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      :cancelText="$t('button.cancel')"
      :okText="$t('button.save')"
      style="top:15%;height: 60%;">
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel">
          {{ $t('button.cancel') }}
        </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="roleModal">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('search.roleName')">
            <a-input :placeholder="$t('title.pleaseInputRoleName')" v-decorator.trim="[ 'name', validatorRules.name]"/>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('title.dataType')">
            <a-select :placeholder="$t('placeholder.pleaseSelect')" v-decorator="[ 'type', validatorRules.type]"
                      style="width:94%">
              <a-select-option value="全部数据">{{ $t('title.dataTypeAll') }}</a-select-option>
              <a-select-option value="本机构数据">{{ $t('title.dataTypeOrg') }}</a-select-option>
              <a-select-option value="个人数据">{{ $t('title.dataTypePersonal') }}</a-select-option>
            </a-select>

            <a-tooltip :title="$t('title.dataTypeTooltip')">
              <a-icon type="question-circle" style="width:6%; padding-left: 5px; font-size: 18px;" />
            </a-tooltip>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('title.priceShield')">
            <j-select-multiple style="width:94%" :placeholder="$t('placeholder.pleaseSelect')"
                               v-model="priceLimitList.value" :options="priceLimitList.options"/>
            <a-tooltip :title="$t('title.priceShieldTooltip')">
              <a-icon type="question-circle" style="width:6%; padding-left: 5px; font-size: 18px;" />
            </a-tooltip>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('search.remark')">
            <a-textarea :rows="1" :placeholder="$t('placeholder.pleaseInputRemark')"
                        v-decorator="[ 'description', validatorRules.description ]" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('search.sort')">
            <a-input placeholder="请输入排序" v-decorator.trim="[ 'sort' ]" />
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import JSelectMultiple from '@/components/jeecg/JSelectMultiple'
import {addRole, checkRole, editRole} from '@/api/api'
import {autoJumpNextInput} from "@/utils/util"
import {mixinDevice} from '@/utils/mixin'

export default {
    name: "RoleModal",
    mixins: [mixinDevice],
    components: {
      JSelectMultiple
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        isReadOnly: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        priceLimitList: {
          options: [
            {'value': '1', 'text': this.$t('title.priceShieldOptions.homePurchasePrice')},
            {'value': '2', 'text': this.$t('title.priceShieldOptions.homeRetailPrice')},
            {'value': '3', 'text': this.$t('title.priceShieldOptions.homeSalePrice')},
            {'value': '4', 'text': this.$t('title.priceShieldOptions.billPurchasePrice')},
            {'value': '5', 'text': this.$t('title.priceShieldOptions.billRetailPrice')},
            {'value': '6', 'text': this.$t('title.priceShieldOptions.billSalePrice')}
          ],
          value: ''
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          name:{
            rules: [
              {required: true, message: this.$t('title.pleaseInputRoleName')},
              {min: 2, max: 30, message: this.$t('message.lengthBetween2And30Characters'), trigger: 'blur'},
              { validator: this.validateRoleName}
            ]
          },
          type:{
            rules: [
              {required: true, message: this.$t('placeholder.pleaseSelect')}
            ]
          },
          description:{
            rules: [
              { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
            ]
          }
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.priceLimitList.value = this.model.priceLimit
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name', 'type', 'sort', 'description'))
          autoJumpNextInput('roleModal')
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            formData.priceLimit = this.priceLimitList.value
            let obj;
            if(!this.model.id){
              obj=addRole(formData);
            }else{
              obj=editRole(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateRoleName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0
        };
        checkRole(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback(this.$t('message.attributeNameAlreadyExists'))
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>
<style scoped>

</style>